package com.spark.lola.controller.logistics;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spark.lola.logistics.entity.ExpensesCategory;
import com.spark.lola.logistics.mapper.ExpensesCategoryMapper;
import com.spark.lola.logistics.service.ExpensesCategoryService;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.common.entity.ShiroUser;
import com.spark.lola.support.common.util.DataBeanUtils;
import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.mybatis.controller.BaseController;
import com.spark.lola.support.mybatis.entity.QueryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Zhi-peng wang
 * @date: 2020/05/07 10:33
 * 货运支出单分类管理接口
 **/
@RestController
@RequestMapping("/api/logistics/expenses-category")
@Api(value = "/api/logistics/expenses-category", tags = "货运支出单分类管理接口")
public class ExpensesCategoryController extends BaseController {

    @Autowired
    private ExpensesCategoryService expensesCategoryService;

    @Autowired
    private ExpensesCategoryMapper expensesCategoryMapper;
    
    @ApiOperation(value = "分页查询分类信息")
    @RequiresPermissions(value = "m5150")
    @RequestMapping(value = "/findBy", method = RequestMethod.POST)
    public ResponseBean findPage(@RequestBody QueryParam<ExpensesCategory> queryParam) {
        IPage<ExpensesCategory> expensesCategoryIPage = expensesCategoryService.findPage(queryParam);
        return ResponseBean.success(expensesCategoryIPage);
    }

    @RequestMapping("/existNo")
    public ResponseBean existNo(@RequestParam String no, @RequestParam(value = "id", defaultValue = "0") Long id) {
        int count = expensesCategoryMapper.existNo(id, no);
        return ResponseBean.success(null, count);
    }

    @ApiOperation(value = "获取分类列表")
    @ApiImplicitParam(name = "key", value = "模糊查询字段，查询车牌", required = true, dataType = "String", paramType = "query")
    @RequestMapping(value = "/map")
    public ResponseBean map(@RequestParam String key) {
        return ResponseBean.success(expensesCategoryService.selectMap(key));
    }

    @ApiOperation(value = "获取分类列表")
    @RequestMapping(value = "/findBi")
    public ResponseBean findBi() {
        return ResponseBean.success(expensesCategoryService.findBi());
    }

    @ApiOperation(value = "获取分类信息详情")
    @ApiImplicitParam(name = "id", value = "分类信息id", required = true, dataType = "long", paramType = "path")
    @RequiresPermissions(value = "m5154")
    @RequestMapping(value = "/findInfo/{id}", method = RequestMethod.GET)
    public ResponseBean findInfo(@PathVariable long id, @SessionAttribute ShiroUser shiroUser) {
        ExpensesCategory expensesCategory = new ExpensesCategory();
        if (id != 0) {
            expensesCategory = expensesCategoryService.getById(id);
        }
        return ResponseBean.success(expensesCategory);
    }

    @ApiOperation(value = "添加/修改分类信息")
    @RequiresPermissions(value = {"m5151", "m5153"}, logical = Logical.OR)
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResponseBean save(@RequestBody ExpensesCategory expensesCategory, @SessionAttribute ShiroUser shiroUser) {
        ExpensesCategory model = new ExpensesCategory();
        if (expensesCategory.getId() != null) {
            model = expensesCategoryService.getById(expensesCategory.getId());
            // 非修改项置空
            model.setUsed(null);
            // 系统修改项赋值
        } else {
            // 设置初始化项
        }
        DataBeanUtils.buildUpdateBean(model, expensesCategory);
        expensesCategoryService.saveOrUpdate(model);
        return ResponseBean.success(model);
    }

    @ApiOperation(value = "启用分类")
    @ApiImplicitParam(name = "nos", value = "分类信息id字符串集", required = true, dataType = "String", paramType = "query")
    @RequiresPermissions(value = "m5155")
    @RequestMapping(value = "/on", method = RequestMethod.GET)
    public ResponseBean on(@RequestParam String nos) {
        if (StringUtils.isBlank(nos)) {
            return ResponseBean.error("字符串集不能为空");
        }
        int count = expensesCategoryService.updateUsedByIds(CommonConstant.Y, nos);
        return ResponseBean.success(null, count);
    }

    @ApiOperation(value = "禁用分类")
    @ApiImplicitParam(name = "nos", value = "分类信息id字符串集", required = true, dataType = "String", paramType = "query")
    @RequiresPermissions(value = "m5156")
    @RequestMapping(value = "/un", method = RequestMethod.GET)
    public ResponseBean un(@RequestParam String nos) {
        if (StringUtils.isBlank(nos)) {
            return ResponseBean.error("id字符串集不能为空");
        }
        int count = expensesCategoryService.updateUsedByIds(CommonConstant.N, nos);
        return ResponseBean.success(null, count);
    }

    @ApiOperation(value = "删除分类信息")
    @ApiImplicitParam(name = "nos", value = "分类信息id集合", required = true, dataType = "Long", paramType = "query")
    @RequiresPermissions(value = "m5152")
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    public ResponseBean del(@RequestParam(value = "nos") List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return ResponseBean.error("id字符串集不能为空");
        }
        if (ids.size() > 1) {
            return ResponseBean.error("禁止批量操作");
        }
        expensesCategoryService.removeById(ids.get(0));
        return ResponseBean.success();
    }

}
